package org.gangcai.mac.shop.activity;

import android.content.Intent;
import android.net.Uri;
import android.net.nsd.NsdManager;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.widget.CardView;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.IntentUtils;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.bumptech.glide.Glide;
import com.orhanobut.dialogplus.DialogPlus;
import com.orhanobut.dialogplus.ViewHolder;

import org.greenrobot.eventbus.EventBus;
import org.gangcai.mac.shop.R;
import org.gangcai.mac.shop.bean.CommonResonseBean;
import org.gangcai.mac.shop.bean.EventBusBaseBean;
import org.gangcai.mac.shop.common.MultiStatusActivity;
import org.gangcai.mac.shop.constants.Constant;
import org.gangcai.mac.shop.oberver.CommonObserver;
import org.gangcai.mac.shop.responsibility.DataResponsibility;
import org.gangcai.mac.shop.utils.MatisseUtils;
import org.gangcai.mac.shop.utils.PromptDialogUtils;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.CompletionService;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import me.leefeng.promptlibrary.OnAdClickListener;
import me.leefeng.promptlibrary.PromptDialog;


public class SettingActivity extends MultiStatusActivity implements View.OnClickListener {

    @BindView(R.id.header)
    CardView header;
    @BindView(R.id.loginout)
    CardView loginout;
    private DialogPlus dialogPlus;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initTitleBarView(titlebar, "设置");
        ButterKnife.bind(this);
    }

    @Override
    public int getContentView() {
        return R.layout.activity_setting;
    }

    @OnClick({R.id.header, R.id.loginout})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.header:
                showDialog();
                break;
            case R.id.loginout:
                showLoginOutDialog();
                PromptDialog prompDialog = PromptDialogUtils.getPrompDialog(this);
                break;
        }
    }

    private void showLoginOutDialog() {
        final DialogPlus dialog = DialogPlus.newDialog(this)
                .setGravity(Gravity.CENTER)
                .setContentBackgroundResource(android.R.color.transparent)
                .setContentWidth(ViewGroup.LayoutParams.WRAP_CONTENT)  // or any custom width ie: 300
                .setContentHeight(ViewGroup.LayoutParams.WRAP_CONTENT)
                .setContentHolder(new ViewHolder(R.layout.logout_dialog_layout))
                .setExpanded(false)  // This will enable the expand feature, (similar to android L share dialog)
                .create();
        View holderView = dialog.getHolderView();
        holderView.findViewById(R.id.loginoutTv).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
                SPUtils.getInstance().clear();
                ActivityUtils.finishAllActivities();

            }
        });

        dialog.show();


    }

    private void showDialog() {

        // This will enable the expand feature, (similar to android L share dialog)
        dialogPlus = DialogPlus.newDialog(this)
                .setContentHolder(new ViewHolder(R.layout.choose_header_dialog_layout))
                .setExpanded(false)  // This will enable the expand feature, (similar to android L share dialog)
                .setContentBackgroundResource(android.R.color.transparent)
                .create();
        View holderView = dialogPlus.getHolderView();
        holderView.findViewById(R.id.photoTv).setOnClickListener(this);
        holderView.findViewById(R.id.cameraTv).setOnClickListener(this);

        dialogPlus.show();
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.photoTv) {
            dialogPlus.dismiss();
            MatisseUtils.pickImage(this, 1, 200);
        } else if (v.getId() == R.id.cameraTv) {
            dialogPlus.dismiss();
           /* File header = new File(Environment.getDataDirectory(), "header");
            Uri uri = Uri.parse(header.getAbsolutePath());
            IntentUtils.getCaptureIntent(uri);*/
            MatisseUtils.pickImage(this, 1, 100);
        }
    }

    private static final String TAG = "SettingActivity";

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (data != null && resultCode == RESULT_OK) {
            List<Uri> images = MatisseUtils.getImages(data);
            Uri uri = images.get(0);
            File file = MatisseUtils.getFile(uri, this.getApplicationContext());
            submitData(file);
        }
    }

    /**
     * 提交用户头像
     *
     * @param file
     */
    private void submitData(File file) {

        prompDialog.showLoading("请等待");
        HashMap<String, String> paramMap = new HashMap<>();
        paramMap.put("uid", SPUtils.getInstance().getString("uid"));
        HashMap<String, File> fileMap = new HashMap<>();
        fileMap.put("photo", file);
        DataResponsibility.getInstance().submitDataToServer("change_photo", paramMap, fileMap, this).subscribe(new CommonObserver<CommonResonseBean>() {
            @Override
            public void onSuccess(CommonResonseBean value) {
                if (value.getCode() == Constant.CODE_SUCC) {
                    //图片上传成功
                    ToastUtils.showShort("用户头像更改成功");
                    EventBus.getDefault().postSticky(new EventBusBaseBean(1));
                    finish();
                }
            }
        });
    }
}
